<template>
  <div class="sights">
    <!-- 搜索 -->
    <div class="search">
      <i class="el-icon-search"></i>
      <input
        type="text"
        v-model="name"
        class="search-input"
        placeholder="搜索景点"
      />
      <el-button
        type="primary"
        class="btn"
        :loading="searchLoading"
        @click="search"
        >搜索</el-button
      >
      <el-link class="reset" :underline="false" type="info" @click="reset"
        >重置</el-link
      >
    </div>
    <!-- 列表 -->
    <div class="list user-common-bg" v-loading="listLoading">
      <div v-if="dataList.length !== 0">
        <CommonItem :list="dataList"></CommonItem>
        <!-- 分页 -->
        <el-pagination
          v-show="dataList.length > 0"
          background
          layout="prev, pager, next"
          :total="total"
          :current-page="currentPage"
          :page-size="pageSize"
          @current-change="handleCurrentChange"
          :page-count="7"
          style="text-align: right; margin-top: 24px"
        >
        </el-pagination>
      </div>
      <div v-else>
        <el-empty description="暂无数据"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
import { getSightsList } from "@/api/user/sights";
import CommonItem from "@/components/CommonItem.vue";
export default {
  name: "SightsView",
  data() {
    return {
      name: "",
      dataList: [],
      page: 1,
      pageSize: 10,
      total: 0,
      currentPage: 1,
      searchLoading: false,
      listLoading: false,
    };
  },
  components: {
    CommonItem,
  },
  created() {
    this.getSightsList();
  },
  methods: {
    // 搜索
    search() {
      if (this.name === "") {
        this.$message.warning("请输入景点名称");
        return;
      }
      try {
        this.searchLoading = true;
        (this.page = 1), (this.currentPage = 1);
        this.getSightsList();
      } finally {
        this.searchLoading = false;
      }
    },
    // 重置
    reset() {
      this.name = "";
      this.page = 1;
      this.currentPage = 1;
      this.getSightsList();
    },
    // 获取景点列表
    async getSightsList() {
      try {
        this.listLoading = true;
        const res = await getSightsList({
          page: this.page,
          pageSize: this.pageSize,
          name: this.name,
        });
        this.dataList = res.data.records;
        this.total = res.data.total;
      } catch {
        this.$message.error("列表获取失败");
      } finally {
        this.listLoading = false;
      }
    },
    // 分页
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getSightsList();
    },
  },
};
</script>

<style lang="less" scoped>
.sights {
  .search {
    position: relative;
    margin-top: 90px;
    width: 100%;
    height: 46px;
    i {
      position: absolute;
      left: 32%;
      top: 32%;
    }
    .search-input {
      padding-left: 40px;
      width: 38%;
      height: 100%;
      border: 1.6px solid rgb(94, 177, 245);
      border-top-left-radius: 8px;
      border-bottom-left-radius: 8px;
      outline: none;
    }
    .btn {
      position: absolute;
      left: 68%;
      top: 0;
      background: linear-gradient(to right, rgb(0, 166, 250), rgb(0, 120, 245));
      color: #fff;
      width: 100px;
      height: 100%;
      border-radius: 0;
      border-top-right-radius: 8px;
      border-bottom-right-radius: 8px;
      letter-spacing: 4px;
      cursor: pointer;
    }
    .reset {
      position: absolute;
      left: 76%;
      top: 4%;
      padding: 10px 20px;
      font-size: 17px;
      border-radius: 8px;
      color: #888;
      background-color: #fff;
      box-shadow: 0 0 3px 1px rgba(0, 0, 0, 0.2);
    }
  }
  .list {
    margin-top: 20px;
    margin-bottom: 20px;
    padding: 20px;
    min-height: 200px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  }
}
</style>